package linuxbox.filelib.bean;

import java.util.*;
import org.apache.log4j.Logger;

import linuxbox.bean.*;
import linuxbox.util.DbAdapter;
import linuxbox.exception.*;
import linuxbox.filelib.logic.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

public class FileLibBean extends BaseBean
{
  protected static Logger logger = Logger.getLogger(FileLibBean.class.getName());

  //each BaseBean derived class should define this
  static protected BeanDef beanDef;

  protected BeanDef getBeanDef()
  {
    return beanDef;
  }

  public FileLibBean()
  {
  }

  public FileLibBean(DbAdapter db)
  {
    super(db);
  }

  protected void setupFields() throws Exception
  {
    if (beanDef == null)
    {
      //Set the table name and the data object associated with the table
      beanDef = new BeanDef("filelib_file", FileLibEntry.class);

      //Setup the fields for the db object
      //name, fieldType, isKey, allowNull, defaultValue, description
      DBField field = beanDef.addKeyField("id", DBField.INT_TYPE, false, "1", "");
      field.setAutoIncrement(true);

      beanDef.addField("name", DBField.STRING_TYPE, false, "", "");
      beanDef.addField("filename", DBField.STRING_TYPE, false, "", "");
      beanDef.addField("ownerid", DBField.INT_TYPE, false, "-1", "");
      beanDef.addField("parent", DBField.INT_TYPE, false, "-1", "");
      beanDef.addField("size", DBField.LONG_TYPE, false, "0", "");
      beanDef.addField("isfolder", DBField.BOOLEAN_TYPE, false, "0", "");
      beanDef.addField("attributes", DBField.LONG_TYPE, false, "2", "");
      beanDef.addField("version", DBField.SHORT_TYPE, true, "0", "");
      beanDef.addField("type", DBField.STRING_TYPE, true, "", "");
      beanDef.addField("description", DBField.STRING_TYPE, true, "", "");
      beanDef.addField("createtime", DBField.TIMESTAMP_TYPE, true, "", "");
      beanDef.addField("lastmodifiedtime", DBField.TIMESTAMP_TYPE, true, "", "");
/*
  id  int4 not null,
  name char(255) not null,
  filename char(255) not null,
  ownerid int4 not null,
  parent int4 not null,
  size int8 not null,
  isfolder int2 not NULL,
  type char(255),
  version int2 ,
  attributes int8 not NULL,
  description char(255),
  createtime timestamp  ,
  lastmodifiedtime timestamp ,

  primary key(id)
*/
    }
  }
  public int [] getParentFolders(int curid)
  {
    if (curid == FileLibHome.ROOT)
      return null;

    int[] folders = null;
    try
    {
      int id = curid;
      makeConnection();

      Vector v = new Vector();
      while (id != FileLibHome.ROOT)
      {
        StringBuffer sql = new StringBuffer();
        sql.append("select parent from ");
        sql.append(getTableName());
        sql.append(" where id=");
        sql.append(id);

        m_db.executeQuery(sql.toString());

        if (m_db.next())
        {
          id = m_db.getInt(1);
          v.addElement(new Integer(id));
        }
        else
        {
          closeConnection();
          throw new TableBeanException("Error get Folder: " + id);
        }
        int vsize = v.size();
        folders = new int[vsize];
        for (int i = 0; i < vsize; i++)
          folders[i] = ((Integer)v.elementAt(i)).intValue();
      }
      closeConnection();
    }
    catch(Exception e)
    {
      logger.error("Error getPathString() when :"+e.getMessage(),e);
    }
    return folders;

  }

  public String getPathString(int curid)
  {
    String path = "/";
    if (FileLibHome.ROOT == curid)
      return "/";
    try
    {
      int id = curid;
      makeConnection();
      while (id != FileLibHome.ROOT)
      {
        StringBuffer sql = new StringBuffer();
        sql.append("select name, parent from ");
        sql.append(getTableName());
        sql.append(" where id=");
        sql.append(id);

        m_db.executeQuery(sql.toString());

        if (m_db.next())
        {
          path = "/" + m_db.getString(1) + path;
          id = m_db.getInt(2);
        }
        else
        {
          closeConnection();
          throw new TableBeanException("Error get Folder: " + id);
        }
      }
      closeConnection();
    }
    catch(Exception e)
    {
      logger.error("Error getPathString() when :"+e.getMessage(),e);
    }
    return path;
  }
}
